import request from "../../utils/http";
import { FamilyMember } from "../../utils/type";
import { anyObj, showConfirm, wxBack, wxToast } from "../../utils/util"

// pages/reservationFirst/reservationFirst.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		query: <anyObj>{},
		address: "",
		formData: {
			"age": <string | number>"",
			"floorNum": <string | number>"",
			"gender": 1,
			"weight": 0,
			"stws": 0,
			"snszcd": <number[]>[],
			"cgqk": <number[]>[],
			"qtjcjb": <number[]>[],
			"isLift": 0,
			"name": "",
			"waterHeaterType": 0,
			"isDefault": 0
		},
		radioList: [
			{
				key: 'weight',
				label: '体重',
				options: [
					{
						value: 0,
						label: '小于50KG'
					},
					{
						value: 1,
						label: '50-75KG'
					},
					{
						value: 2,
						label: '75-90KG'
					},
					{
						value: 3,
						label: '90KG以上'
					}
				]
			},
			{
				key: 'stws',
				label: '身体外伤',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '轻度',
						value: 1
					},
					{
						label: '中度',
						value: 2
					},
					{
						label: '其他',
						value: 3
					},
				]
			},
			{
				key: 'isLift',
				label: '是否电梯',
				options: [
					{
						label: '电梯',
						value: '1'
					},
					{
						label: '楼梯',
						value: 0
					},
				]
			},
			{
				key: 'waterHeaterType',
				label: '热水器类型',
				options: [
					{
						label: '电热水器',
						value: 0
					},
					{
						label: '燃气热水器',
						value: 1
					},
				]
			}
		],
		checkboxList: [
			{
				key: 'snszcd',
				label: '失能失智程度',
				options: [
					{
						label: '全失能',
						value: 0
					},
					{
						label: '半失能',
						value: 1
					},
					{
						label: '搀扶下可行走',
						value: 2
					},
					{
						label: '其他',
						value: 3
					}
				]
			},
			{
				key: 'cgqk',
				label: '插管情况',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '胃管',
						value: 1
					},
					{
						label: '尿管',
						value: 2
					},
					{
						label: '其他',
						value: 3
					},
				]
			},
			{
				key: 'qtjcjb',
				label: '其他基础疾病',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '脑梗',
						value: 1
					},
					{
						label: '心梗',
						value: 2
					},
					{
						label: '高血压',
						value: 3
					},
					{
						label: '糖尿病',
						value: 4
					},
					{
						label: '其他',
						value: 5
					},
				]
			}
		]
	},

	async getFamilyMember() {
		try {
			const familyMember: FamilyMember = await request({
				url: '/api/customers/familyMembers/getOneDetail',
				method: 'GET',
				query: {
					id: this.data.query.familyMemberId
				},
				errToast: true,
				showLoading: {title: '正在获取用户信息...'}
			});
			const formData = this.data.formData;
			formData.age = familyMember.age;
			formData.floorNum = familyMember.floorNum || 0;
			formData.gender = familyMember.gender;
			formData.isDefault = familyMember.isDefault;
			formData.name = familyMember.name;
			formData.isLift = familyMember.isLit;
			formData.waterHeaterType = familyMember.waterHeaterType;
			
			if (familyMember.healthInfo) {
				const healthInfoList = familyMember.healthInfo.split(";");
				healthInfoList.forEach(healthInfo => {
					const [key, value] = healthInfo.split(':');
					console.log(key, value)

					switch (key) {
						case "TZ":
							formData.weight = Number(value);
							break;
						case "STWS":
							formData.stws = Number(value);
							break;
						case "SNSZCD":
							if (value) {
								formData.snszcd = value.split(',').map(val => {
									return Number(val);
								});
							}
							break;
						case "CGQK":
							if (value) {
								formData.cgqk = value.split(',').map(val => {
									return Number(val);
								});
							}
							break;
						case "QTJCJB":
							if (value) {
								formData.qtjcjb = value.split(',').map(val => {
									return Number(val);
								});
							}
							break;
					
						default:
							break;
					}
				});
			}
			this.setData({formData});
		} catch (error) {
			
		}
	},

	async onChangeForm(e: WechatMiniprogram.CustomEvent) {
		console.log("e: ", e)
		const formData = this.data.formData as any;
		formData[e.currentTarget.dataset.key] = e.detail.value;
		console.log("formData: ", formData)
		this.setData({
			formData
		});
	},

	async submit() {
		const formData = this.data.formData;

		if (!formData.age) {
			return wxToast({icon: 'error', title: '请输入年龄'});
		}
		if (!formData.floorNum) {
			return wxToast({icon: 'error', title: '请输入楼层'});
		}
		if (!formData.name) {
			return wxToast({icon: 'error', title: '请输入被服务人姓名'});
		}
		await showConfirm({content: '确定提交?'});
		try {
			const healthInfo = [`TZ:${formData.weight}`, `STWS:${formData.stws}`];
			if (formData.snszcd.length > 0) {
				healthInfo.push(`SNSZCD:${formData.snszcd.join(",")}`);
			}
			if (formData.cgqk.length > 0) {
				healthInfo.push(`CGQK:${formData.cgqk.join(",")}`);
			}
			if (formData.qtjcjb.length > 0) {
				healthInfo.push(`QTJCJB:${formData.qtjcjb.join(",")}`);
			}
			await request({
				url: '/api/customers/familyMembers/save',
				method: 'POST',
				contentTypeForm: false,
				errToast: true,
				showLoading: {title: '正在提交...'},
				data: {
					"age": formData.age,
					"floorNum": formData.floorNum,
					"gender": formData.gender,
					"healthInfo": healthInfo.join(";"),
					"id": this.data.query.familyMemberId,
					"isDefault": formData.isDefault,
					"isLift": formData.isLift,
					"name": formData.name,
					"waterHeaterType": formData.waterHeaterType
				}
			});
			wxToast({icon: 'success', title: '提交成功'});
			wxBack({});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query: anyObj) {
		this.setData({
			query
		});
		if (query.familyMemberId) {
			this.getFamilyMember();
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})